<template>
  <!-- 车厂变更经销商信息使用 -->
  <div class="wrapper">
    <div style="position: relative;">
      <i
        class="el-icon-close"
        @click="closeDialogTop()"
        style="position:absolute;cursor: pointer; font-size:22px; top: -48px; right: 0px"
      ></i>
    </div>
    <!-- <Anchor class="mainAnchor" :labelList="labels" activeId="row-01" labelWidth="80">
    <div class="line" id="row-01" style="min-height:780px">-->
    <BreakSpace :label="labels[0].title" :containerStyle="{ marginBottom: '10px' }" />
    <infoBase
      ref="infoBase"
      :formType="'value.type'"
      :formOptions="value"
      v-model="formData"
      @formchange="infoBaseChange"
    />
    <!-- </div>
    <div class="line" id="row-02">-->
    <BreakSpace :label="labels[1].title" :containerStyle="{ marginBottom: '10px' }" />
    <infoProduct
      ref="infoProduct"
      :formType="'edite'"
      :formOptions="value"
      v-model="formData"
      @formchange="infoProductChange"
    />
    <!-- </div>
    <div class="line" id="row-03" style="min-height:780px">-->
    <BreakSpace :label="labels[2].title" :containerStyle="{ marginBottom: '10px' }" />
    <infoPower
      ref="infoPower"
      :formType="'edite'"
      :formOptions="value"
      v-model="formData"
      @formchange="infoPowerChange"
    />
    <!-- </div>
    <div class="line" id="row-04">-->
    <BreakSpace :label="labels[3].title" :containerStyle="{ marginBottom: '10px' }" />
    <infoCompany
      ref="infoCompany"
      :formType="value.type"
      :formOptions="value"
      v-model="formData"
      @formchange="infoCompanyChange"
    />
    <!-- </div>
    <div class="line" id="row-05">-->
    <BreakSpace :label="labels[4].title" :containerStyle="{ marginBottom: '10px' }" />
    <infoStock
      ref="infoStock"
      :formType="value.type"
      :formOptions="value"
      v-model="formData"
      @formchange="infoStockChange"
    />
    <!-- </div>
    <div class="line" id="row-06">-->
    <BreakSpace :label="labels[5].title" :containerStyle="{ marginBottom: '10px' }" />
    <infoFinance
      ref="infoFinance"
      :formType="value.type"
      :formOptions="value"
      v-model="formData"
      @formchange="infoFinanceChange"
    />
    <!-- </div> -->
    <!-- <div class="line" id="row-07">
				<infoOpinion ref="infoOpinion" :formType="value.type" v-model="formData" @formchange="infoOpinionChange"/>
    </div>-->
    <!-- </Anchor> -->
    <div class="dialogBottmView">
      <el-button @click="closeDialogTop">关 闭</el-button>
      <el-button @click="determineDialog()" v-if="value.type!=='show'" type="primary">保 存</el-button>
      <!-- <el-button @click="determineDialog()" v-if="value.type!=='show'" :disabled="this.formData.status === this.types.DEALER_INFO_STATUS_ENABLE" type="primary">确 定</el-button> -->
      <!-- <el-button @click="determineDialog()" v-if="value.type!=='show'" :disabled="okDisabled" type="primary">确 定</el-button> -->
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';

import autoLoding from '@/utils/loding'; //等待框
import infoBase from '../dealerFormView/infoBase'; //基本信息
import infoProduct from '../dealerFormView/infoProduct'; //产品授权
import infoPower from '../dealerFormView/infoPower'; //授权信息

import infoCompany from '../dealerForm/infoCompany'; //公司信息
import infoStock from '../dealerForm/infoStock'; //股东信息
import infoFinance from '../dealerForm/infoFinance'; //财务信息
// import infoOpinion from '../dealerForm/infoOpinion'; //审核信息

import { getDetails } from '@/api/cms-api/origin/originInfo';
import { addAllData, editAllData, addDetermine, editDetermineChange } from '@/api/cms-api/origin/originInfo/OEM';
import * as types from '@/api/cms-api/origin/originInfo/types';
import { dealerCommon } from '@/api/cms-api/origin/commonDealerType'; //经销商类型工具类
import { isArray } from 'util';

import vm from './bridge';

export default {
  mixins: [authority, dictionary, autoLoding, dealerCommon],
  components: {
    infoBase,
    infoProduct,
    infoPower,
    infoCompany,
    infoStock,
    infoFinance
  },
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      okDisabled: false,
      formData: {},
      labels: [
        { title: '基本信息', id: 'row-01' },
        { title: '产品授权', id: 'row-02' },
        { title: '授权信息', id: 'row-03' },
        { title: '公司信息', id: 'row-04' },
        { title: '股东信息', id: 'row-05' },
        { title: '财务信息', id: 'row-06' }
      ],
      types: types
    };
  },
  mounted() {
    this.loadingVm = this.openLoding('.cover-container');
    if (this.value.type != 'add') {
      //按ID获取数据
      this.init();
    } else {
      vm.$emit('changeDealerType', types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER);
      this.closeLoding(this.loadingVm);
    }
  },
  methods: {
    async init() {
      let res = await getDetails(this.value.id, {});
      if (res.resultCode === 200) {
        //转换附件为list<Sting>
        this.formData = this.transformAllEnclosureString(res.data);
        this.formData.inputNumber = '';
        this.formData.inputChar = '';
        this.$nextTick(() => {
          this.$refs.infoBase.init();
          this.$refs.infoProduct.init();
          this.$refs.infoPower.init();
          this.$refs.infoCompany.initPre();
          this.$refs.infoStock.init();
          this.$refs.infoFinance.init();
          this.closeLoding(this.loadingVm);
        });
      }
    },
    //确定（验证并提交）
    determineDialog() {
      console.log(1);
      this.$refs.infoBase.saveDialog();
    },
    //取消操作
    closeDialogTop() {
      this.$confirm('确认关闭窗口吗？您所做的操作将不会被保存！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$emit('close', { type: 'none', visible: false });
      });
    },
    infoBaseChange(data) {
      console.log(2);
      this.$refs.infoProduct.saveDialog();
    },
    infoProductChange(data) {
      console.log(3);
      this.$refs.infoPower.saveDialog();
    },
    infoPowerChange(data) {
      console.log(4);
      this.$refs.infoCompany.saveDialog();
    },
    infoCompanyChange(data) {
      console.log(5);
      this.$refs.infoStock.saveDialog();
    },
    infoStockChange(data) {
      console.log(5);
      this.$refs.infoFinance.saveDialog();
    },
    infoFinanceChange(_data) {
      console.log(5);
      
      //防止点确认报错后点击保存造成状态不正常的问题
      let data = Object.assign({}, _data);
      //不生成账号和经销商code
      this.loadingVm = this.openLoding('.cover-container');
      data.changeType = 'OEM'; //车厂端变更
      //调用提交
      this.saveDetermine(data);
    },
    //转化表单的附件 转换为list<Map<String,Object>>（提交前使用）
    //不再使用由IMG转FILE
    transformAllEnclosure(data) {
      let rdata = Object.assign({}, data);

      rdata.designFeeEnclosureList = this.transformEnclosureList(rdata.designFeeEnclosureList); //设计费附件
      rdata.cashDepositEnclosureList = this.transformEnclosureList(rdata.cashDepositEnclosureList); //保证金附件

      rdata.businessLicenseEnclosureList = this.transformEnclosureList(rdata.businessLicenseEnclosureList); //营业执照扫描件
      rdata.legalRepresentativeEnclosureList = this.transformEnclosureList(rdata.legalRepresentativeEnclosureList); //法定代表人附件
      rdata.firstPaymentEnclosureList = this.transformEnclosureList(rdata.firstPaymentEnclosureList); //首笔提车款附件

      rdata.openAccountProveEnclosureList = this.transformEnclosureList(rdata.openAccountProveEnclosureList); //开户证明附件

      rdata.licenceEnclosureList = this.transformEnclosureList(rdata.licenceEnclosureList); //许可证附件
      return rdata;
    },
    //转化表单的附件 转换为list<Sting>（回显使用）
    transformAllEnclosureString(data) {
      let rdata = data;

      rdata.designFeeEnclosureList = this.transformEnclosureStringList(rdata.designFeeEnclosureList); //设计费附件
      rdata.cashDepositEnclosureList = this.transformEnclosureStringList(rdata.cashDepositEnclosureList); //保证金附件

      rdata.businessLicenseEnclosureList = this.transformEnclosureStringList(rdata.businessLicenseEnclosureList); //营业执照扫描件
      rdata.legalRepresentativeEnclosureList = this.transformEnclosureStringList(rdata.legalRepresentativeEnclosureList); //法定代表人附件
      rdata.firstPaymentEnclosureList = this.transformEnclosureStringList(rdata.firstPaymentEnclosureList); //首笔提车款附件

      rdata.openAccountProveEnclosureList = this.transformEnclosureStringList(rdata.openAccountProveEnclosureList); //开户证明附件

      rdata.licenceEnclosureList = this.transformEnclosureStringList(rdata.licenceEnclosureList); //许可证附件

      //经销商形式
      let dealerAttachAuthList = [];
      if (isArray(rdata.dealerAttachAuthList)) {
        rdata.dealerAttachAuthList.forEach(x => {
          dealerAttachAuthList.push(x.financeCompanyId);
        });
      }
      rdata.dealerAttachAuthList = dealerAttachAuthList;

      //车系
      let dealerAttachSeriesList = [];
      if (isArray(rdata.dealerAttachSeriesList)) {
        rdata.dealerAttachSeriesList.forEach(x => {
          dealerAttachSeriesList.push(x.seriesId);
        });
      }
      rdata.dealerAttachSeriesList = dealerAttachSeriesList;

      //车型
      let dealerAttachModelList = [];
      if (isArray(rdata.dealerAttachModelList)) {
        rdata.dealerAttachModelList.forEach(x => {
          dealerAttachModelList.push(x.modelId);
        });
      }
      rdata.dealerAttachModelList = dealerAttachModelList;
      return rdata;
    },
    //转换成附件对象 转换为list<Map<String,Object>>
    transformEnclosureList(list) {
      let fileUrlList = [];
      if (!isArray(list)) {
        return fileUrlList;
      }
      //不再使用由IMG转FILE
      // for (let i = 0; i < list.length; i++) {
      // 	let fileUrl = list[i];
      // 	fileUrl = fileUrl.replace(/\\/g,"/");
      // 	fileUrlList.push(Object.assign({},{"name":fileUrl.substring(fileUrl.lastIndexOf('/')+1)},{"url":fileUrl}));
      // }
      // return fileUrlList;
      return list;
    },
    //转换成附件对象(回显) 转换为list<Sting>
    transformEnclosureStringList(list) {
      let fileUrlList = [];
      if (!isArray(list)) {
        return fileUrlList;
      }
      //不再使用由IMG转FILE
      // for (let i = 0; i < list.length; i++) {
      // 	let fileUrl = list[i];
      // 	fileUrlList.push(fileUrl.fileId);
      // }
      list.forEach(x => {
        fileUrlList.push({ name: x.fileName, url: x.fileId });
      });
      return fileUrlList;
    },
    //获取from的数据。包括key，value，和label
    getChangeData(){
       let changeData = [];
      this.$refs.infoBase.initForm.forEach(x =>{
        
        changeData.push({name:x.fieldName,label:x.label,initialValue:x.initialValue,itemList:x.itemList})
      });
      this.$refs.infoCompany.initForm.forEach(x =>{
        changeData.push({name:x.fieldName,label:x.label,initialValue:x.initialValue,itemList:x.itemList})
      });
      this.$refs.infoFinance.initForm.forEach(x =>{
        changeData.push({name:x.fieldName,label:x.label,initialValue:x.initialValue,itemList:x.itemList})
      });
      this.$refs.infoPower.initForm.forEach(x =>{
        changeData.push({name:x.fieldName,label:x.label,initialValue:x.initialValue,itemList:x.itemList})
      });
      debugger
      this.$refs.infoProduct.initForm.forEach(x =>{
        
        changeData.push({name:x.fieldName,label:x.label,initialValue:x.initialValue,itemList:x.itemList})
      });
      this.$refs.infoStock.initForm.forEach(x =>{
        changeData.push({name:x.fieldName,label:x.label,initialValue:x.initialValue,itemList:x.itemList})
      });
      return changeData;
    },
    async saveDetermine(data) {
      let changeData = this.getChangeData();
      changeData = Object.assign({"changeData":changeData}, data);
      let res = {};
      res = await editDetermineChange(this.value.id, changeData);
      if (res.resultCode === 200) {
        this.$notify.success({ title: '提示信息', message: '更新成功!' });
        this.closeLoding(this.loadingVm);
        this.$emit('close', 'refresh');
      } else {
        this.closeLoding(this.loadingVm);
        this.$notify.error({ title: '提示信息', message: res.errMsg || '更新失败!' });
      }
    }
  }
};
</script>

<style scoped>
.wrapper,
.line {
  height: calc(100vh - 135px);
}
.line {
  overflow-x: hidden;
  overflow-y: auto;
}
</style>
